package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.atguigu.entity.Dict;
import com.atguigu.mapper.DictMapper;
import com.atguigu.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 翟文海
 * @Date: 2022/10/28/028 15:56
 * @Version: 1.0
 * @Description:
 **/
@Service(interfaceClass = DictService.class)
public class DictServiceImpl implements DictService {
    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private JedisPool jedisPool;
    @Override
    public List<Map<String, Object>> findZnodes(Long parentId) {
        List<Dict> data = dictMapper.findZnodes(parentId);
        List<Map<String, Object>> mapList = data.stream().map(dict -> {
            Map<String, Object> map = new HashMap<>();
            map.put("name", dict.getName());
            map.put("id", dict.getId());
            map.put("isParent", !CollectionUtils.isEmpty(dictMapper.findZnodes(dict.getId())));
            return map;
        }).collect(Collectors.toList());
        return mapList;
    }

    @Override
    public List<Dict> findByDictCode(String parentDictCode) {
        //看redis缓存中是否有数据
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();

            String value = jedis.get("shf:dict:parentDictCode:"+parentDictCode);
            if (!StringUtils.isEmpty(value)) {
                return JSON.parseArray(value, Dict.class);
            }
            //没有的话就从数据库中查找，再存到redis中
            List<Dict> dicts = dictMapper.findByDictCode(parentDictCode);
            String jsonString = JSON.toJSONString(dicts);
            jedis.set("shf:dict:parentDictCode:"+parentDictCode,jsonString);
            return dicts;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
            if (jedis.isConnected()) {
                jedis.disconnect();
            }
        }
    }

    @Override
    public List<Dict> findDictListByParentId(Long parentId) {
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            String s = jedis.get("shf:dict:parentId:" + parentId);
            if (!StringUtils.isEmpty(s)) {
                return JSON.parseArray(s,Dict.class);
            }
            List<Dict> dictListByParentId = dictMapper.findDictListByParentId(parentId);
            jedis.set("shf:dict:parentId:" + parentId, JSON.toJSONString(dictListByParentId));
            return dictListByParentId;
        }finally {
            if (jedis != null) {
                jedis.close();
            }
            if (jedis.isConnected()) {
                jedis.disconnect();
            }
        }
    }
}
